/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.hellodoctor.slayer.model;

import com.hellodoctor.slayer.service.AppointmentLocalServiceUtil;
import com.hellodoctor.slayer.service.ClpSerializer;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.DateUtil;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;

import java.io.Serializable;

import java.lang.reflect.Method;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Shahbaj
 */
public class AppointmentClp extends BaseModelImpl<Appointment>
	implements Appointment {
	public AppointmentClp() {
	}

	@Override
	public Class<?> getModelClass() {
		return Appointment.class;
	}

	@Override
	public String getModelClassName() {
		return Appointment.class.getName();
	}

	@Override
	public long getPrimaryKey() {
		return _appointmentId;
	}

	@Override
	public void setPrimaryKey(long primaryKey) {
		setAppointmentId(primaryKey);
	}

	@Override
	public Serializable getPrimaryKeyObj() {
		return _appointmentId;
	}

	@Override
	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey(((Long)primaryKeyObj).longValue());
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("appointmentId", getAppointmentId());
		attributes.put("doctorId", getDoctorId());
		attributes.put("patientId", getPatientId());
		attributes.put("appointmentDate", getAppointmentDate());
		attributes.put("appointmentTime", getAppointmentTime());
		attributes.put("status", getStatus());
		attributes.put("createDate", getCreateDate());
		attributes.put("modifiedDate", getModifiedDate());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		Long appointmentId = (Long)attributes.get("appointmentId");

		if (appointmentId != null) {
			setAppointmentId(appointmentId);
		}

		Long doctorId = (Long)attributes.get("doctorId");

		if (doctorId != null) {
			setDoctorId(doctorId);
		}

		Long patientId = (Long)attributes.get("patientId");

		if (patientId != null) {
			setPatientId(patientId);
		}

		Date appointmentDate = (Date)attributes.get("appointmentDate");

		if (appointmentDate != null) {
			setAppointmentDate(appointmentDate);
		}

		Integer appointmentTime = (Integer)attributes.get("appointmentTime");

		if (appointmentTime != null) {
			setAppointmentTime(appointmentTime);
		}

		Integer status = (Integer)attributes.get("status");

		if (status != null) {
			setStatus(status);
		}

		Date createDate = (Date)attributes.get("createDate");

		if (createDate != null) {
			setCreateDate(createDate);
		}

		Date modifiedDate = (Date)attributes.get("modifiedDate");

		if (modifiedDate != null) {
			setModifiedDate(modifiedDate);
		}
	}

	@Override
	public long getAppointmentId() {
		return _appointmentId;
	}

	@Override
	public void setAppointmentId(long appointmentId) {
		_appointmentId = appointmentId;

		if (_appointmentRemoteModel != null) {
			try {
				Class<?> clazz = _appointmentRemoteModel.getClass();

				Method method = clazz.getMethod("setAppointmentId", long.class);

				method.invoke(_appointmentRemoteModel, appointmentId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getDoctorId() {
		return _doctorId;
	}

	@Override
	public void setDoctorId(long doctorId) {
		_doctorId = doctorId;

		if (_appointmentRemoteModel != null) {
			try {
				Class<?> clazz = _appointmentRemoteModel.getClass();

				Method method = clazz.getMethod("setDoctorId", long.class);

				method.invoke(_appointmentRemoteModel, doctorId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getPatientId() {
		return _patientId;
	}

	@Override
	public void setPatientId(long patientId) {
		_patientId = patientId;

		if (_appointmentRemoteModel != null) {
			try {
				Class<?> clazz = _appointmentRemoteModel.getClass();

				Method method = clazz.getMethod("setPatientId", long.class);

				method.invoke(_appointmentRemoteModel, patientId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public Date getAppointmentDate() {
		return _appointmentDate;
	}

	@Override
	public void setAppointmentDate(Date appointmentDate) {
		_appointmentDate = appointmentDate;

		if (_appointmentRemoteModel != null) {
			try {
				Class<?> clazz = _appointmentRemoteModel.getClass();

				Method method = clazz.getMethod("setAppointmentDate", Date.class);

				method.invoke(_appointmentRemoteModel, appointmentDate);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public int getAppointmentTime() {
		return _appointmentTime;
	}

	@Override
	public void setAppointmentTime(int appointmentTime) {
		_appointmentTime = appointmentTime;

		if (_appointmentRemoteModel != null) {
			try {
				Class<?> clazz = _appointmentRemoteModel.getClass();

				Method method = clazz.getMethod("setAppointmentTime", int.class);

				method.invoke(_appointmentRemoteModel, appointmentTime);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public int getStatus() {
		return _status;
	}

	@Override
	public void setStatus(int status) {
		_status = status;

		if (_appointmentRemoteModel != null) {
			try {
				Class<?> clazz = _appointmentRemoteModel.getClass();

				Method method = clazz.getMethod("setStatus", int.class);

				method.invoke(_appointmentRemoteModel, status);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public Date getCreateDate() {
		return _createDate;
	}

	@Override
	public void setCreateDate(Date createDate) {
		_createDate = createDate;

		if (_appointmentRemoteModel != null) {
			try {
				Class<?> clazz = _appointmentRemoteModel.getClass();

				Method method = clazz.getMethod("setCreateDate", Date.class);

				method.invoke(_appointmentRemoteModel, createDate);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public Date getModifiedDate() {
		return _modifiedDate;
	}

	@Override
	public void setModifiedDate(Date modifiedDate) {
		_modifiedDate = modifiedDate;

		if (_appointmentRemoteModel != null) {
			try {
				Class<?> clazz = _appointmentRemoteModel.getClass();

				Method method = clazz.getMethod("setModifiedDate", Date.class);

				method.invoke(_appointmentRemoteModel, modifiedDate);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public BaseModel<?> getAppointmentRemoteModel() {
		return _appointmentRemoteModel;
	}

	public void setAppointmentRemoteModel(BaseModel<?> appointmentRemoteModel) {
		_appointmentRemoteModel = appointmentRemoteModel;
	}

	public Object invokeOnRemoteModel(String methodName,
		Class<?>[] parameterTypes, Object[] parameterValues)
		throws Exception {
		Object[] remoteParameterValues = new Object[parameterValues.length];

		for (int i = 0; i < parameterValues.length; i++) {
			if (parameterValues[i] != null) {
				remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
			}
		}

		Class<?> remoteModelClass = _appointmentRemoteModel.getClass();

		ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

		Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

		for (int i = 0; i < parameterTypes.length; i++) {
			if (parameterTypes[i].isPrimitive()) {
				remoteParameterTypes[i] = parameterTypes[i];
			}
			else {
				String parameterTypeName = parameterTypes[i].getName();

				remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
			}
		}

		Method method = remoteModelClass.getMethod(methodName,
				remoteParameterTypes);

		Object returnValue = method.invoke(_appointmentRemoteModel,
				remoteParameterValues);

		if (returnValue != null) {
			returnValue = ClpSerializer.translateOutput(returnValue);
		}

		return returnValue;
	}

	@Override
	public void persist() throws SystemException {
		if (this.isNew()) {
			AppointmentLocalServiceUtil.addAppointment(this);
		}
		else {
			AppointmentLocalServiceUtil.updateAppointment(this);
		}
	}

	@Override
	public Appointment toEscapedModel() {
		return (Appointment)ProxyUtil.newProxyInstance(Appointment.class.getClassLoader(),
			new Class[] { Appointment.class }, new AutoEscapeBeanHandler(this));
	}

	@Override
	public Object clone() {
		AppointmentClp clone = new AppointmentClp();

		clone.setAppointmentId(getAppointmentId());
		clone.setDoctorId(getDoctorId());
		clone.setPatientId(getPatientId());
		clone.setAppointmentDate(getAppointmentDate());
		clone.setAppointmentTime(getAppointmentTime());
		clone.setStatus(getStatus());
		clone.setCreateDate(getCreateDate());
		clone.setModifiedDate(getModifiedDate());

		return clone;
	}

	@Override
	public int compareTo(Appointment appointment) {
		int value = 0;

		value = DateUtil.compareTo(getAppointmentDate(),
				appointment.getAppointmentDate());

		if (value != 0) {
			return value;
		}

		if (getAppointmentTime() < appointment.getAppointmentTime()) {
			value = -1;
		}
		else if (getAppointmentTime() > appointment.getAppointmentTime()) {
			value = 1;
		}
		else {
			value = 0;
		}

		if (value != 0) {
			return value;
		}

		if (getStatus() < appointment.getStatus()) {
			value = -1;
		}
		else if (getStatus() > appointment.getStatus()) {
			value = 1;
		}
		else {
			value = 0;
		}

		if (value != 0) {
			return value;
		}

		return 0;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!(obj instanceof AppointmentClp)) {
			return false;
		}

		AppointmentClp appointment = (AppointmentClp)obj;

		long primaryKey = appointment.getPrimaryKey();

		if (getPrimaryKey() == primaryKey) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return (int)getPrimaryKey();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(17);

		sb.append("{appointmentId=");
		sb.append(getAppointmentId());
		sb.append(", doctorId=");
		sb.append(getDoctorId());
		sb.append(", patientId=");
		sb.append(getPatientId());
		sb.append(", appointmentDate=");
		sb.append(getAppointmentDate());
		sb.append(", appointmentTime=");
		sb.append(getAppointmentTime());
		sb.append(", status=");
		sb.append(getStatus());
		sb.append(", createDate=");
		sb.append(getCreateDate());
		sb.append(", modifiedDate=");
		sb.append(getModifiedDate());
		sb.append("}");

		return sb.toString();
	}

	@Override
	public String toXmlString() {
		StringBundler sb = new StringBundler(28);

		sb.append("<model><model-name>");
		sb.append("com.hellodoctor.slayer.model.Appointment");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>appointmentId</column-name><column-value><![CDATA[");
		sb.append(getAppointmentId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>doctorId</column-name><column-value><![CDATA[");
		sb.append(getDoctorId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>patientId</column-name><column-value><![CDATA[");
		sb.append(getPatientId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>appointmentDate</column-name><column-value><![CDATA[");
		sb.append(getAppointmentDate());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>appointmentTime</column-name><column-value><![CDATA[");
		sb.append(getAppointmentTime());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>status</column-name><column-value><![CDATA[");
		sb.append(getStatus());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>createDate</column-name><column-value><![CDATA[");
		sb.append(getCreateDate());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>modifiedDate</column-name><column-value><![CDATA[");
		sb.append(getModifiedDate());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private long _appointmentId;
	private long _doctorId;
	private long _patientId;
	private Date _appointmentDate;
	private int _appointmentTime;
	private int _status;
	private Date _createDate;
	private Date _modifiedDate;
	private BaseModel<?> _appointmentRemoteModel;
}